/**
 * 
 */
package com.yst.metho.web.application.project;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.NamingException;

import com.yst.metho.web.application.interfaces.IListingViewModel;
import com.yst.metho.web.application.project.actions.CreateProject;
import com.yst.metho.web.application.project.actions.EditProjectAction;
import com.yst.metho.web.application.project.actions.EditTeamProjectAction;
import com.yst.metho.web.application.utils.AbstractSuiviComparator;
import com.yst.metho.web.application.utils.AbstractSuiviTableModel;
import com.yst.metho.web.application.utils.Locator;
import com.yst.suivi.data.project.Project;

/**
 * @author perso
 * 
 */
public class ProjectListModel extends AbstractSuiviTableModel implements IListingViewModel
{
	private class ProjectComparator extends AbstractSuiviComparator
	{

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
		 */
		public int compare(Object o1, Object o2)
		{
			Project u1 = (Project) o1;
			Project u2 = (Project) o2;
			int comparaison = 0;
			switch (getColumn())
			{
			case 0:
				comparaison = u1.getCode().compareToIgnoreCase(u2.getCode());
				break;
			case 1:
				comparaison = u1.getNom().compareToIgnoreCase(u2.getNom());
				break;
			case 2:
				if (u1.getStatus() > u2.getStatus())
				{
					comparaison = 1;
				}
				else
				{
					comparaison = 2;
				}
				break;
			default:
				break;
			}
			if (!isAscending())
			{
				comparaison = -1 * comparaison;
			}
			return comparaison;
		}

	}

	private String[] mColumnsName =
	{ "Code", "Nom", "Etat", "Mode", "Equipe" };

	// private Collection mProjectsList;

	public ProjectListModel() throws NamingException, FinderException, CreateException
	{
		super();
		setComparator(new ProjectComparator());
		refresh();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.metho.web.application.interfaces.IListingViewModel#getToolbarAction()
	 */
	public List getToolbarAction()
	{
		ArrayList actions = new ArrayList();
		actions.add(new CreateProject(this));
		actions.add(new EditProjectAction(this));
		actions.add(new EditTeamProjectAction(this));
		return actions;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.metho.web.application.interfaces.IListingViewModel#refresh()
	 */
	public void refresh()
	{
		try
		{
			mList = Locator.getProjectView().findAllProjects();
			fireTableDataChanged();
		}
		catch (NamingException e)
		{
			throw new RuntimeException(e);
		}
		catch (FinderException e)
		{
			throw new RuntimeException(e);
		}
		catch (CreateException e)
		{
			throw new RuntimeException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nextapp.echo2.app.table.AbstractTableModel#getColumnName(int)
	 */
	public String getColumnName(int arg0)
	{
		return mColumnsName[arg0];
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nextapp.echo2.app.table.TableModel#getColumnCount()
	 */
	public int getColumnCount()
	{
		return mColumnsName.length;
	}
}
